﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Olympic_email_to_future.Module;
using Olympic_email_to_future.IDAL;
using System.Data.SqlClient;

namespace Olympic_email_to_future.DAL
{
    class FeedbackDAL : IFeedbackInfo
    {
        FeedbackInfoDataContext cxt = new FeedbackInfoDataContext("Data Source=LHX\\SQLEXPRESS;Initial Catalog=Olympic;Integrated Security=True");

        //后台update
        public bool update(int id, FeedbackInfo feedbackinfo)
        {
            FeedbackInfo feedback = cxt.FeedbackInfo.SingleOrDefault(c => c.feedbackId == id);
            feedback.adminReply = feedbackinfo.adminReply;
            feedback.adminIsReply = 1;
            cxt.SubmitChanges();
            return true;
        }

        //后台delete
        public bool delete(int id, FeedbackInfo feedbackinfo)
        {
            FeedbackInfo feedback = cxt.FeedbackInfo.SingleOrDefault(c => c.feedbackId == id);
            cxt.FeedbackInfo.DeleteOnSubmit(feedback);
            cxt.SubmitChanges();
            return true;
        }

        // 返回已回复反馈 ，用户后台管理
        public IList<FeedbackInfo> getFeedbackY()
        {
            IList<FeedbackInfo> fbs = new List<FeedbackInfo>();
            var result = from p in cxt.FeedbackInfo where p.adminIsReply.Equals(1) select p;
            foreach (Module.FeedbackInfo p in result)
            {
                FeedbackInfo fb = new FeedbackInfo();
                fb.feedbackId = p.feedbackId;
                fb.userId = p.userId;
                fb.feedbackContent = p.feedbackContent;
                fb.adminReply = p.adminReply;
                fbs.Add(fb);
            }
            if (fbs != null)
                return fbs;
            else
                return null;
        }

        // 返回反馈
        public IList<FeedbackInfo> getFeedback()
        {
            IList<FeedbackInfo> fbs = new List<FeedbackInfo>();
            var result = from p in cxt.FeedbackInfo select p;
            foreach (Module.FeedbackInfo p in result)
            {
                FeedbackInfo fb = new FeedbackInfo();
                fb.feedbackId = p.feedbackId;
                fb.userId = p.userId;
                fb.feedbackContent = p.feedbackContent;
                fb.adminReply = p.adminReply;
                fbs.Add(fb);
            }
            if (fbs != null)
                return fbs;
            else
                return null;
        }

        // 返回未回复反馈 ，用户后台管理
        public IList<FeedbackInfo> getFeedbackN()
        {
            IList<FeedbackInfo> fbs = new List<FeedbackInfo>();
            var result = from p in cxt.FeedbackInfo where p.adminIsReply.Equals(0) select p;
            foreach (Module.FeedbackInfo p in result)
            {
                FeedbackInfo fb = new FeedbackInfo();
                fb.feedbackId = p.feedbackId;
                fb.userId = p.userId;
                fb.feedbackContent = p.feedbackContent;
                fb.adminReply = p.adminReply;
                fbs.Add(fb);
            }
            if (fbs != null)
                return fbs;
            else
                return null;
        }


        public bool feedback(FeedbackInfo fb, int id)
        {
            FeedbackInfo fbinfo = new FeedbackInfo();
            fbinfo.feedbackContent = fb.feedbackContent;
            fbinfo.feedbackDate = DateTime.Now;
            fbinfo.userId = id;
            cxt.FeedbackInfo.InsertOnSubmit(fbinfo);
            cxt.SubmitChanges();
            return true;
        }
    }
}
